﻿
$(window).load(function () {
    dobtnaction("getAll");
    var tem31 = LS.get('CURR_RECEIPT_PROVINCE_NME') || ' ';
    $("#spnToProvince").html(tem31 + '<i class="mui_addr_icon">');

    var $imgul = $(".gb-img-nav li");
    $imgul.hover(function () {
        $("#J_ImgBooth").attr("src", $(this).find(" img").attr("src"));
        //        $("#J_ImgBooth").stop().hide().attr("src", $(this).find(" img").attr("src")).fadeIn(300);
    }).live("click",function () {
        $("#J_ImgBooth").attr("src", $(this).find(" img").attr("src"));
        //        $("#J_ImgBooth").stop().hide().attr("src", $(this).find(" img").attr("src")).fadeIn(300);
    });

    $("#J_Amount .mui-amount-increase").click(function () {
        $("#J_Amount input").val(parseInt($("#J_Amount input").val() || 0) + 1);
        validatestock();
    });
    $("#J_Amount .mui-amount-decrease").click(function () {
        if (parseInt($("#J_Amount input").val() || 0) > 1) { $("#J_Amount input").val(parseInt($("#J_Amount input").val() || 0) - 1); }
        validatestock();
    });
    $("#J_Amount input").blur(function () {
        if (parseInt($(this).val() || 0) < 1) {
            $(this).val("1");
        }
        validatestock();
    });


    $("#s-header li").click(function () {
        var $self = $(this);
        $("#s-header .gb-selected-nav").removeClass("gb-selected-nav");
        $self.addClass("gb-selected-nav"); 
    });
     

    $("#J_LinkBuy").click(function () {
        subOrder("0");
    });
    $("#J_LinkBasket").click(function () {
        subOrder("1");
    });
    $("#xshop_collection_href").click(function () {

        $.loginCallBack(function () {
            $.loadHtml('/pgajax.axd?ADD_SHOP_COLL=1&ID=' + $("#txtDealerId").val()).done(function (_rets) {
                if (_rets && _rets == '1') {
                    $.ploadFile("layer", function () {
                        layer.msg('已成功收藏店铺！', 1, { type: 1, shade: false });
                    });
                }
            });
        }, true);
    });
});

$(function () {
    $("#divNormsView .J_TSaleProp li").live("click", function () {
        if ($(this).hasClass("tb-out-of-stock")) {
            return;
        }
        var $self = $(this);
        $self.prevAll().removeClass("tb-selected").find(" i").remove();
        $self.nextAll().removeClass("tb-selected").find(" i").remove();
        $self.addClass("tb-selected").find(" a").after("<i>已选中</i>");
        //        $self.parent().attr("sel-data-value", $self.attr("data-value"));
        var isok = validSaleSel();
        //updateqty
        dobtnaction("doupdateqtyprice");
        if (isok && $(".tb-attention").length > 0) {
            $(".J_PanelCloser").trigger('click');
            //            $("#J_LinkBuy").addClass("tb-act tb-btn-inbox");
            //            $("#J_LinkBasket").addClass("tb-act tb-btn-inbox");
        }
    });
     
});
function validSaleSel() {
    var isok = true; 
    if ($("#J_TSaleProp_p1 li").length > 0) {
        if ($("#J_TSaleProp_p1 .tb-selected").length <= 0) {
            isok = false;
        }
    }
    if ($("#J_TSaleProp_p2 li").length > 0) {
        if ($("#J_TSaleProp_p2 .tb-selected").length <= 0) {
            isok = false;
        }
    } 

//    $("#divNormsView .J_TSaleProp").each(function () {
//        if ($(this).attr("sel-data-value") == "") {
//            isok = false;
//        }
//    });
    return isok;
};
function validatestock() {
    var odQty = parseInt($("#J_Amount input").val());
    if (odQty && odQty > parseInt($('#J_EmStock').html())) {
        $("#J_Amount input").val($('#J_EmStock').html());
    }
};
function setnormslist(spnum, snormsnme, bllock) {
    if (bllock) {
        $("#J_TSaleProp_p" + spnum + " li[data-value='" + snormsnme + "']").removeClass("tb-selected").addClass("tb-out-of-stock");
    } else {
        $("#J_TSaleProp_p" + spnum + " li[data-value='" + snormsnme + "']").removeClass("tb-out-of-stock");
    }
};
function subOrder(stp) {
    var odQty = $("#J_Amount input").val();
    var isok = validSaleSel();
    if (parseInt(odQty) <= 0) {
        alert("请选择要购买的商品数量！");
        return;
    }
    var prquery = "q=" + odQty;
    if ($("#J_TSaleProp_p1 .tb-selected").length > 0) {
        prquery += "&p1=" + ($("#J_TSaleProp_p1 .tb-selected").attr("data-value").replace('#', '%23').replace('&', '%26').replace('=', '%3D').replace('+', '%2b'));
    }
    if ($("#J_TSaleProp_p2 .tb-selected").length > 0) {
        prquery += "&p2=" + ($("#J_TSaleProp_p2 .tb-selected").attr("data-value").replace('#', '%23').replace('&', '%26').replace('=', '%3D').replace('+', '%2b'));
    }
//    $("#divNormsView .J_TSaleProp").each(function () {
//        if ($(this).attr("sel-data-value") == "") {
//            isok = false;
//        }
//        prquery += "&" + $(this).attr("data-property") + "=" + $(this).attr("sel-data-value");
//    });
    if (!isok) {
        alert("请选择您要的商品信息");
        return;
//        var $skutem = $(".tb-key").removeClass("tb-attention").addClass("tb-attention").find(".tb-sku");
//        $skutem.prev().remove();
//        $skutem.before('<p class="tb-note-title">请选择您要的商品信息<b class="J_PanelCloser">x</b></p>');
//        $skutem.prev().find(".J_PanelCloser").click(function () {
//            $(this).parents(".tb-key").removeClass("tb-attention");
//            $(this).remove();
//        });
    }
    else {
        if (stp == '0') {
            window.location.href = "/mb_CustOrder.htm?s=0&g=" + $("#txtId").val() + "&" + prquery + "&p=" + (LS.get('CURR_RECEIPT_PROVINCE_ID') || '');
        } else {
            jQuery.loadHtml('/pgajax.axd?GETLOGIN_ST=1').done(function (_rets) { if (_rets == "0" || _rets == "") { window.location.href = '/mp_Login.htm'; return; } });
        jQuery.loginCallBack(function () {
            jQuery.loadHtml('/pgajax.axd?ADDCART_DATA=1&REF_LIST=0&' + "g=" + $("#txtId").val() + "&" + prquery).done(function (_rets) {
                if (!_rets) { return; }
                var arrqty = _rets.split(',');
                var _retqty1 = 0;
                var _retqty2 = 0;
                if (arrqty && arrqty.length > 0) {
                    _retqty1 = parseInt(arrqty[0]);
                    _retqty2 = parseInt(arrqty[1]);
                }
                //                alert(_retqty1);
                //                alert(_retqty2);
                jQuery.ploadFile("layer", function () {
                    layer.msg('已成功加入！', 1, { type: 1, shade: false });
                });
                //                    _self._showFlash(simg);
            });
        }, false);
//            jQuery.pageRbar.addToCart($("#J_ImgBooth").attr("src"),"g=" + $("#txtId").val() + "&" + prquery);
        }
    }
};
function dobtnaction(sAction) { 
    var _jsParam = new jsonRow();
    _jsParam.AddCell("ACTION", sAction);
    _jsParam.AddCell("ID", $("#txtId").val());
    _jsParam.AddCell("TO_PROV_ID", LS.get('CURR_RECEIPT_PROVINCE_ID')||'');
    _jsParam.AddCell("LOGIS_ID", $("#txtLogisId").val());
//    _jsParam.AddCell("LOGIS_NME", $("#txtLogisNme").val());
    _jsParam.AddCell("DEALER_ID", $("#txtDealerId").val());
    _jsParam.AddCell("SEL_QTY", $("#J_Amount input").val());
    if (sAction == "doupdateqtyprice") {
        var isok = true;

        if ($("#J_TSaleProp_p1 .tb-selected").length > 0) {
            _jsParam.AddCell("SEL_p1", $("#J_TSaleProp_p1 .tb-selected").attr("data-value"));  
        }
        if ($("#J_TSaleProp_p2 .tb-selected").length > 0) {
            _jsParam.AddCell("SEL_p2", $("#J_TSaleProp_p2 .tb-selected").attr("data-value"));  
        }


//        $("#divNormsView .J_TSaleProp").each(function () {
//            if ($(this).attr("sel-data-value") == "") {
//                isok = false;
//            }
//            _jsParam.AddCell("SEL_" + $(this).attr("data-property"), $(this).attr("sel-data-value")); 
//        });


//        var $p1 = $("#divNormsView .J_TSaleProp[data-property='p1']");
//        if ($p1 && $p1.length > 0) {
//            _jsParam.AddCell("HAS_P1", '1');
//            _jsParam.AddCell("SEL_P1", $p1.attr("sel-data-value"));
//        }
//        var $p2 = $("#divNormsView .J_TSaleProp[data-property='p2']");
//        if ($p2 && $p2.length > 0) {
//            _jsParam.AddCell("HAS_P2", '1');
//            _jsParam.AddCell("SEL_P2", $p2.attr("sel-data-value"));
//        }
    }

    ajaxComm(_jsParam);
};


function doRateToPageNm(_pNum) { 
        var _jsParam = new jsonRow();
        _jsParam.AddCell("ACTION", "doFinderRateList");
        var chkratenum = $(".rate-filter").find("input:radio[name='radiogrounp']:checked").val();
        if (chkratenum != null && chkratenum != undefined) {
            _jsParam.AddCell("FINDER_TYPE", chkratenum);
        }
        if ($("#J_R7").attr("checked") == true || $("#J_R7").attr("checked") == "checked") {
            _jsParam.AddCell("HAS_CONTENT", "1");
        }

        _jsParam.AddCell("ID", $("#txtId").val());
        _jsParam.AddCell("NEW_PAGE_NUM", _pNum || $("#Ratecurrpage").val());
        _jsParam.AddCell("PAGE_ROW_COUNT", "20");
        _jsParam.AddCell("PAGE_ROW_SORT", "");

        ajaxComm(_jsParam); 
};

function updateRateTableHtml(_data) {
    var tpl = HtmlUtil.htmlDecode(document.getElementById('rate_list_temp').innerHTML);
    laytpl(tpl).render(_data, function (render) {
        $("#ratetbody").empty().append(render);
    });
    $("#Ratecurrpage").val(_data.page);
    createPaging('ratepaginator', _data.page, _data.total, function (_topagenum) { doRateToPageNm(_topagenum); }); 

};

function doDealToPageNm(_pNum) { 
        var _jsParam = new jsonRow();
        _jsParam.AddCell("ACTION", "doFinderBuyerList"); 

        _jsParam.AddCell("ID", $("#txtId").val());
        _jsParam.AddCell("NEW_PAGE_NUM", _pNum || $("#Dealcurrpage").val());
        _jsParam.AddCell("PAGE_ROW_COUNT", "20");
        _jsParam.AddCell("PAGE_ROW_SORT", "");

        ajaxComm(_jsParam); 
};

function updateDealRdTableHtml(_data) {
    var tpl = HtmlUtil.htmlDecode(document.getElementById('buyer_list_temp').innerHTML);
    laytpl(tpl).render(_data, function (render) {
        $("#J_showBuyerList").empty().append(render);
    });
    $("#Dealcurrpage").val(_data.page);
    createPaging('buyerlistpage', _data.page, _data.total, function (_topagenum) { doDealToPageNm(_topagenum); }); 

};
